package com.saifan.wyy_ov.ui.onlishop;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CommentBean;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.IGetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.ui.zoomimage.ZoomImageActivity;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

/**
 * @author WangLi
 * @version V1.0
 * @date 2015年3月18日 下午7:13:26
 * @Description 评论列表
 */

public class CommentsActivity extends BaseActivity {

    private RelativeLayout loadlay;
    private boolean footerFlag = false;// 是否添加了加载更多

    private IGetData getData;
    private CommonDataBean requestBean;
    private List<CommentBean> commentBeans;
    private int page = 0;
    private CommonAdapter<CommentBean> adapter;
    private ListView listView;
    private RefreshLayout refreshLayout;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_pulltorefresh);
        requestBean = new CommonDataBean();
        requestBean.setID(getIntent().getIntExtra("ID", -1));
        getData = new GetData();
        findViews();
        getDataInit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);

    }

    // 初始化控件
    @SuppressLint("InflateParams")
    private void findViews() {
        loadlay = (RelativeLayout) findViewById(R.id.loading_lay);
        listView = (ListView) findViewById(R.id.listView);
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        commentBeans = new ArrayList<CommentBean>();
        adapter = new CommonAdapter<CommentBean>(CommentsActivity.this, commentBeans, R.layout.comment_item) {

            @Override
            public void convert(ViewHolderHelper helper, final CommentBean item) {
                helper.setText(R.id.client_info_nickname, StringUtil.returnShow(item.getEvaluationPeople()));
                helper.setText(R.id.time, formatTime(item.getTime()));
                helper.setText(R.id.details, StringUtil.returnShow(item.getContent()));
                if (StringUtil.isNullOrEmpty(item.getEvaluationPeople()) || item.getEvaluationPeople().startsWith("匿名")) {
                    ImageView imgView = helper.getView(R.id.client_info_header);
                    ImgUtil.DisPlayImg( R.drawable.anonymity_head,imgView,CommentsActivity.this);
                } else {
                    helper.setImageByUrl(R.id.client_info_header, item.getHeadPhoto());
                }

                if (item.getPhotos() != null) {
                    int size = item.getPhotos().size();
                    if (size < 1) {
                        LinearLayout imgLayout = helper.getView(R.id.img_lay);
                        imgLayout.setVisibility(View.GONE);
                    } else {
                        LinearLayout imgLayout = helper.getView(R.id.img_lay);
                        imgLayout.setVisibility(View.VISIBLE);
                        for (int i = 1; i < 6; i++) {
                            if (i <= size) {
                                int id = getResources().getIdentifier("img" + i, "id", CommentsActivity.this.getPackageName());
                                helper.getView(id).setVisibility(View.VISIBLE);
                                ImageView img = helper.getView(id);
                                final int position = i - 1;
                                img.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(CommentsActivity.this, ZoomImageActivity.class);

                                        intent.putExtra("position", position);
                                        ArrayList<String> urls = new ArrayList<String>();
                                        for (int i = 0; i < item.getPhotos().size(); i++) {
                                            urls.add(item.getPhotos().get(i).getUrl());
                                        }
                                        intent.putStringArrayListExtra("img", urls);
                                        startActivity(intent);
                                    }
                                });
                                helper.setImageByUrl(id, item.getPhotos().get(i - 1).getUrl());
                            } else {
                                int id = getResources().getIdentifier("img" + i, "id", CommentsActivity.this.getPackageName());
                                helper.getView(id).setVisibility(View.INVISIBLE);
                            }
                        }

                    }
                } else {
                    LinearLayout imgLayout = helper.getView(R.id.img_lay);
                    imgLayout.setVisibility(View.GONE);
                }
            }
        };
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                getDataInit();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub
                refreshLayout.setLoading(true);
                page++;
                getDataInit();

            }
        });
        listView.setAdapter(adapter);
    }

    // 获取商品列表
    private void getDataInit() {
        requestBean.setPage(page);
        if (commentBeans.isEmpty()) {
            loadlay.setVisibility(View.VISIBLE);
        }
        if (requestBean.getID() == -1) {
            ToastUtil.showToast(CommentsActivity.this, "暂无评论信息");
            loadlay.setVisibility(View.GONE);
            return;
        }
        getData.getDataJson(CommentsActivity.this, Api.Comment.GETEVALUATION, requestBean, "", new ResponseData() {
            @Override
            public void onSuccess(String json) {
                loadlay.setVisibility(View.GONE);
                List<CommentBean> list = (new Gson()).fromJson(json, new TypeToken<List<CommentBean>>() {
                }.getType());
                if (list!=null&&list.size() > 0) {
                    if (page == 0) {
                        commentBeans = list;
                    } else {
                        commentBeans.addAll(list);
                    }
                    adapter.mDatas = commentBeans;
                    adapter.notifyDataSetChanged();

                } else {
                    if (page == 0) {
                        ToastUtil.showToast(CommentsActivity.this, "暂无评论信息");
                    } else {
                        ToastUtil.showToast(CommentsActivity.this, "暂无更多评论信息");
                        page--;
                    }
                }

                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);

            }

            @Override
            public void onFailed(String str) {
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);

            }
        });
    }

    //格式化时间
    private String formatTime(String time) {
        if (StringUtil.isNullOrEmpty(time)) {
            return "很久以前";
        }
        return time.replaceAll("T", " ");
    }
}
